﻿package airdnd.controls {
	import flash.events.*;
	import flash.ui.Mouse;
	import flash.display.MovieClip;
	import airdnd.WorldDesign;
	import airdnd.world.*;
	
	import com.inruntime.utils.*;
	import flash.utils.ByteArray;
	import flash.net.FileReference;
	
	public class MapControls extends MovieClip{
		private var global:Global = Global.getInstance();
		//gets all the global stored variables
		
		public static var colorPick:myCP = new myCP();
		public static var moveMouseCursor = new moveCursor();
		
		public static var paintMode:Boolean = false;
		public static var moveMode:Boolean = false;
		public static var zoomLevel:int = 0;

		public function MapControls() {
			WorldDesign.moveButton.addEventListener(MouseEvent.CLICK, worldMove);
			WorldDesign.paintButton.addEventListener(MouseEvent.CLICK, paintWorld);
			WorldDesign.saveButton.addEventListener(MouseEvent.CLICK, saveWorld);
			WorldDesign.clearButton.addEventListener(MouseEvent.CLICK, clearWorld);
		}
		
		public function worldMove(evt:MouseEvent){
			Mouse.hide();
			if(paintMode == true){
				paintMode = false;
			}
			moveMode = true;
			global.stage.addChild(moveMouseCursor);
			moveMouseCursor.x = evt.stageX;
			moveMouseCursor.y = evt.stageY;
			global.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveMoveHandler);
			global.world.addEventListener(MouseEvent.MOUSE_DOWN, dragWorld);
			global.world.addEventListener(MouseEvent.MOUSE_UP, dropWorld);
			global.world.addEventListener(MouseEvent.MOUSE_WHEEL, zoomWorld);
		}
		
		function zoomWorld(evt:MouseEvent){
			
			if(evt.delta == 3){
				if(zoomLevel != 2){
					global.world.scaleX = global.world.scaleX + 0.2;
					global.world.scaleY = global.world.scaleY + 0.2;
					zoomLevel++;
				}
			} else {
				if(zoomLevel != -2){
					global.world.scaleX = global.world.scaleX - 0.2;
					global.world.scaleY = global.world.scaleY - 0.2;
					zoomLevel = zoomLevel - 1;
					trace(zoomLevel);
				}
			}
		}

		function dragWorld(event:MouseEvent):void {
			global.world.startDrag();
		}

		function dropWorld(event:MouseEvent):void {
			global.world.stopDrag();
		}
		
		function paintWorld(evt:MouseEvent):void{
			Mouse.show();
			if(moveMode == true){
				global.stage.removeChild(moveMouseCursor);
				moveMode = false;
			}
			paintMode = true;
			global.stage.addChild(colorPick);
			colorPick.x = 952;
			colorPick.y = 53;
			global.world.removeEventListener(MouseEvent.MOUSE_DOWN, dragWorld);
			global.world.removeEventListener(MouseEvent.MOUSE_UP, dropWorld);
			global.world.removeEventListener(MouseEvent.MOUSE_WHEEL, zoomWorld);
			colorPick.addEventListener (Event.CHANGE, colorChanged);
		}
		
		function colorChanged (e:Event):void { 
			global.tileCol = e.target.selectedColor;
		}
		
		function mouseMoveMoveHandler(evt:MouseEvent):void{
			moveMouseCursor.x = evt.stageX;
			moveMouseCursor.y = evt.stageY;
		}
		
		function saveWorld(evt:MouseEvent):void{
			var xmlDump:String = "<xml>";
			for(var i:Number=0; i<WorldHandler.tileArray.length; i++){
				xmlDump = xmlDump.concat('<tile xcor="'+WorldHandler.tileArray[i][1]+'" ycor="'+WorldHandler.tileArray[i][2]+'" xposition="'+WorldHandler.tileArray[i][3]+'" yposition="'+WorldHandler.tileArray[i][4]+'" color="'+WorldHandler.tileArray[i][5]+'" />');
				
			}
			xmlDump = xmlDump.concat('</xml>');
			var _xml:XML = new XML(xmlDump);//puts the string into xml
			
			//saves as an xml file
			var ba:ByteArray = new ByteArray();
            ba.writeUTFBytes(_xml);
			var fr:FileReference = new FileReference();
			fr.save(ba, "map.xml");
			//trace(xmlDump);
		}
		
		function clearWorld(evt:MouseEvent):void{
			WorldDesign.instance.redrawMap();
		}
	}
}